package com.yuhang.learnandroid.chapter07

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.EditText
import com.yuhang.learnandroid.R
import kotlinx.android.synthetic.main.fragment_crime.*


/**
 * A simple [Fragment] subclass.
 * Activities that contain this fragment must implement the
 * [CrimeFragment.OnFragmentInteractionListener] interface
 * to handle interaction events.
 * Use the [CrimeFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class CrimeFragment : Fragment() {

    private var crime: Crime = Crime()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        var v =  inflater!!.inflate(R.layout.fragment_crime, container, false)

        var titleField = v.findViewById<EditText>(R.id.crime_title)

        titleField.addTextChangedListener(object: TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                crime.title = s.toString()
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

        var dateBtn = v.findViewById<Button>(R.id.crime_date)
        dateBtn.text = DateFormat.format("yyyy-mm-dd HH:mm:ss",crime.date)
        dateBtn.isEnabled = false

        var solvedCheckBox = v.findViewById<CheckBox>(R.id.crime_solved)
        solvedCheckBox.setOnCheckedChangeListener(object: CompoundButton.OnCheckedChangeListener{
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                crime.solved = isChecked
            }
        })

        return v
    }

}// Required empty public constructor
